/*
 * 添加物料采购信息
 * 参数   交易码：trancode=42122
 * 
 */

package org.ccams.tran;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.ccams.domain.TPlanindetail;
import org.ccams.domain.TPlaninhead;
import org.ccams.domain.User;
import org.ccams.util.StrHelper;
import org.ccams.util.Utils;
import org.ccams.web.UserSession;
import org.ccams.web.WebBeanFactory;
import org.json.JSONArray;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * 添加物料采购信息
 * 
 * @author liuguangshuai@gmail.com
 * @系统名称：定制化辅助生成制造系统
 */
public class Tran42122 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int pid = StrHelper.getInt(request.getParameter("pid"), 0);
		int provuid = StrHelper.getInt(request.getParameter("provuid"), 0);		
		String tranmoney=(String) request.getParameter("tranmoney");	
		String comedate=(String) request.getParameter("comedate");
		String validdate=(String) request.getParameter("validdate");		
		String remark = (String) request.getParameter("remark");
		String params = (String) request.getParameter("json");		
		//获得订单创建人
		HttpSession session = request.getSession();
		UserSession userSession = (UserSession)session.getAttribute("userSession");
		User user = userSession.getUser();
		TPlaninhead tp = new TPlaninhead();
		tp.setOperdate(Utils.getCurrentDateTime());
		tp.setComedate(Utils.stringToDate(comedate, "yyyy-MM-dd"));
		tp.setValiddate(Utils.stringToDate(validdate, "yyyy-MM-dd"));
		tp.setPid(pid);
		tp.setPlanno(Utils.getID());
		tp.setProvuid(provuid);
		tp.setRemark(remark);
		tp.setCwflag(0);
		tp.setApid(0);
		tp.setAdate(Utils.getCurrentDateTime());
		tp.setTranmoney(StrHelper.getBigDecimal(tranmoney, 0));
		JSONArray salesordersd = new JSONArray(params);
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";			
		DataSourceTransactionManager txm = (DataSourceTransactionManager) WebBeanFactory.getBean("transactionManager",request);
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = txm.getTransaction(def);
		try{
			getCcamsService().getTplaninheadDao().insert(tp);//添加订单主表信息
			long hid = tp.getId();
			//添加订单从表信息
			for(int i=0;i<salesordersd.length();i++){
				org.json.JSONObject ordersd = salesordersd.optJSONObject(i);
				TPlanindetail t = new TPlanindetail();
					t.setHid(hid);
					t.setAmount(StrHelper.getBigDecimal(ordersd.getString("amount"), 0));
					t.setAttrib1(StrHelper.getBigDecimal(ordersd.getString("attrib1"), 0));
					t.setAttrib2(StrHelper.getBigDecimal(ordersd.getString("attrib2"), 0));
					t.setAttrib3(StrHelper.getBigDecimal(ordersd.getString("attrib3"), 0));
					t.setAttrib4(StrHelper.getBigDecimal(ordersd.getString("attrib4"), 0));
					t.setAttrib5(StrHelper.getBigDecimal(ordersd.getString("attrib5"), 0));
					t.setBarcode(Utils.getID());
					t.setComeamount(StrHelper.getBigDecimal(ordersd.getString("comeamount"), 0));
					t.setFactcode(Utils.getID());
					t.setFlag(0);
					t.setInprice(StrHelper.getBigDecimal(ordersd.getString("inprice"), 0));
					t.setMaterielid(ordersd.getInt("materielid"));
					t.setModidate(Utils.getCurrentDateTime());
					t.setModipid(user.getId());
					t.setOrd(i+1);
					t.setRemark(ordersd.getString("remark"));
					t.setType(ordersd.getInt("type"));
					t.setTaxprice(StrHelper.getBigDecimal(ordersd.getString("taxprice"), 0));
					t.setState(0);
					t.setUsername(ordersd.getString("username"));
					getCcamsService().getTplanindetailDao().insert(t);//执行添加			
			}
			success = true;
			message = "物料采购添加成功";
		}catch(Exception ex){
			txm.rollback(status);
            ex.printStackTrace();
            success = false;
			message = "物料采购添加失败";
		}finally{
			txm.commit(status);
		}		
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
